﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="ImportWeb.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ImportWeb type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.ContentMigration
{
    using System;
    using System.Collections.Generic;

    using Engine;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.ContentMigration;
    using SharePoint.Wss.Webs;

    using Utils;

    [CollaborisClass(
            Title =
                "Imports a Web from a CMP.",
            ReleaseStatus =
                ReleaseStatus.Stable,
            DisableDocumentation =
                false,
            ContactAuthorEmail =
                "Support@Collaboris.co.uk",
            Summary =
                "Imports a web (SPWeb) from a Content Migration Package (CMP).",
            Remarks =
                "<p>The 'Do' method for this action uses the standard SharePoint content migration " +
                "API to achieve the import. " +
                "It is highly recommended that the server is running a minimum of SharePoint " +
                "Service Pack 1.</p>" +
                @"This action can easily be paired with <a href=""C:\Temp\safDoc\Collaboris.Saf.Actions.Wss.ContentMigration.ExportWeb.html"">Export Web</a>" +
                " to move content between sites and/or farms.",
              ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C735}")]
    [Serializable]
    public class ImportWeb : WSSActionBase<WebImportSettings>
    {
        ContentMigrationHelper helper = new ContentMigrationHelper();


        #region Methods (7)

        /// <summary>
        /// When called causes the Web passed in to be Imported to a 
        /// CMP Package to a specified file.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, WebImportSettings entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");


            using (SPSite targetSite = TargetWeb.Site)
            {
                helper.ImportToWeb(
                    entity.ImportSettings, TargetWeb, targetSite.Url, TargetWeb.Url, entity.DeleteWebIfExists);
            }

            this.Trace.TraceMethodEnd("DoAction");

            return ProcessStatus.Success;
        }


        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">Web of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, WebImportSettings entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            // ValidateStringForNull(entity.WebName, "WebImportSettings.WebName", ref errors);
            
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }

        #endregion Methods
    }
}